package com.supplychainapi.controller;

import com.joindata.inf.common.basic.exceptions.ServiceException;
import com.supplychainapi.common.ApiVersion;
import com.supplychainapi.common.RestResponse;
import com.supplychainapi.domain.LoginCache;
import com.supplychainapi.domain.SeqNoType;
import com.supplychainapi.domain.treeBuild.TreeBuildType;
import com.supplychainapi.model.ScmTbfinderlist;
import com.supplychainapi.service.ITbfinderlistService;
import com.supplychainapi.service.TbSeqNoService;
import com.supplychainapi.utils.PathUitls;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@RequestMapping(PathUitls.ROOT_PHONE + PathUitls.ROOT_VERSION + "/public")
@RestController
@Api(value = "PublicController", description = "公用 设置自动编码获取是否自动编码接口")
public class PublicController extends BaseController {

    @Autowired
    ITbfinderlistService tbfinderlistService;

    @Autowired
    TbSeqNoService seqNoService;


    @ApiVersion(1)
    @ApiOperation(value="设置自动获取编码", notes="")
    @RequestMapping(value = "/setAutoGetCode", method= RequestMethod.GET)
    public RestResponse setAutoGetCode(@ApiIgnore LoginCache loginCache, @ApiParam(value = "是否为自动获取编码0不是1是", type = "string", defaultValue = "0") @RequestParam String auto, @ApiParam("type") @RequestParam SeqNoType seqNoType) throws ServiceException {
        ScmTbfinderlist tbfinderlist=new ScmTbfinderlist();
        tbfinderlist.setFsShopGUID(loginCache.getFsShopGUID());
        tbfinderlist.setFsNote("物料自动编码");
        tbfinderlist.setFsUserId(loginCache.getFsUserId());
        tbfinderlist.setFsData(auto);
        tbfinderlist.setFsFinderId(seqNoType.getCode() + "");
        tbfinderlistService.save(tbfinderlist);
        if("1".equals(auto)){
            return RestResponse.success(seqNoService.getNo(seqNoType.getCode(), loginCache.getFsShopGUID()));
        }
        return RestResponse.success();
    }


    @ApiVersion(1)
    @ApiOperation(value="获取是否自动获取编码", notes="")
    @RequestMapping(value = "/getIsAutoCode", method=RequestMethod.GET)
    public RestResponse<String> getIsAutoCode(@ApiIgnore LoginCache loginCache,@ApiParam("type") @RequestParam TreeBuildType treeBuildType) throws ServiceException {
        ScmTbfinderlist tbfinderlist=new ScmTbfinderlist();
        tbfinderlist.setFsShopGUID(loginCache.getFsShopGUID());
        tbfinderlist.setFsFinderId(treeBuildType.name());
        ScmTbfinderlist result=tbfinderlistService.select(tbfinderlist);
        String auto="0";
        if(null!=result && StringUtils.isNotBlank(result.getFsData())){
            auto=result.getFsData();
        }
        return RestResponse.success(auto);
    }


    //获取自动编码接口
    @ApiVersion(1)
    @ApiOperation(value = "自动编码获取", notes = "")
    @RequestMapping(value = "/getAutoCode", method = RequestMethod.GET)
    public RestResponse getAutoCode(@ApiIgnore LoginCache loginCache, @ApiParam("类型") @RequestParam SeqNoType type) throws ServiceException {
        return RestResponse.success(seqNoService.getNo(type.getCode(), loginCache.getFsShopGUID()));
    }
}
